package com.keya.pull;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * @author huangshuqiang
 * @version 1.0
 * @description: TODO
 * @date 2022/8/31 14:15
 */
@NoArgsConstructor
@Data
@Accessors(chain = true)
@ApiModel(value = "DatasourceConfigVO", description = "数据源配置对象")
public class DatasourceConfigVO implements Serializable {

    @NotNull(message = "findPacsEnabled不能为空")
    @ApiModelProperty(value = "定时查询 PACS：false-禁用，true-启用")
    private Boolean findPacsEnabled;

    @ApiModelProperty(value = "设备id列表")
    private List<Integer> deviceIds;

    @NotNull(message = "findViewEnable不能为空")
    @ApiModelProperty(value = "定时查询数据库视图：false-禁用，true-启用")
    private Boolean findViewEnabled;

    @ApiModelProperty(value = "数据源名称")
    private String name;

    @ApiModelProperty(value = "数据库jdbc地址")
    private String url;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "设备id")
    private Integer deviceId;

    @ApiModelProperty(value = "sql语句")
    private String sql;

    @NotNull(message = "pullStudyEnabled不能为空")
    @ApiModelProperty(value = "自动获取 PACS 数据：false-禁用，true-启用")
    private Boolean pullStudyEnabled;

    @ApiModelProperty(value = "StudyDescription 阻止列表")
    private String[] pullStudyBlacklist;

    @ApiModelProperty(value = "StudyDescription 允许列表")
    private String[] pullStudyWhitelist;

    @NotNull
    @ApiModelProperty(value = "抓取时间点（单位：分钟），默认 8, 15, 30, 120")
    private int[] pullStudyRetryBackoff;

    public DatasourceConfigVO mergeVo(DatasourceViewVO datasourceView, DatasourcePacsVO datasourcePacs, PullStudyCronVO pullStudy) {
        if (datasourceView != null) {
            this.setUrl(datasourceView.getUrl())
                    .setUsername(datasourceView.getUsername())
                    .setPassword(datasourceView.getPassword())
                    .setDeviceId(datasourceView.getDeviceId())
                    .setSql(datasourceView.getSql())
                    .setFindViewEnabled(datasourceView.getEnabled());
        }
        if (datasourcePacs != null) {
            this.setDeviceIds(datasourcePacs.getDeviceIds())
                    .setFindPacsEnabled(datasourcePacs.getEnabled());
        }
        if (pullStudy != null) {
            this.setPullStudyEnabled(pullStudy.getEnabled())
                    .setPullStudyBlacklist(pullStudy.getBlacklist())
                    .setPullStudyWhitelist(pullStudy.getWhitelist())
                    .setPullStudyRetryBackoff(pullStudy.getRetryBackoff());
        }
        return this;
    }

    public DatasourceViewVO genDatasourceViewVO() {
        return new DatasourceViewVO()
                .setUrl(this.getUrl())
                .setUsername(this.getUsername())
                .setPassword(this.getPassword())
                .setDeviceId(this.getDeviceId())
                .setSql(this.getSql())
                .setEnabled(this.getFindViewEnabled());
    }

    public DatasourcePacsVO genDatasourcePacsVO() {
        return new DatasourcePacsVO()
                .setDeviceIds(this.getDeviceIds())
                .setEnabled(this.getFindPacsEnabled());
    }

    public PullStudyCronVO genPullStudyCronVO() {
        return new PullStudyCronVO()
                .setBlacklist(this.getPullStudyBlacklist())
                .setWhitelist(this.getPullStudyWhitelist())
                .setEnabled(this.getPullStudyEnabled())
                .setRetryBackoff(this.getPullStudyRetryBackoff());
    }
}
